# -*- encoding: utf-8 -*-
# author:lmolhw
# datetime:2020-11-17 17:25

"""
文件说明：
    增强图像
"""

import numpy as np
import cv2

# img = cv2.imread('demo_images/WX20200915-142819@2x.png', cv2.IMREAD_GRAYSCALE)
img = cv2.imread('/data1/lhw/workspace/OCR_Test/打车票/IMG_20190225_1532112.jpg')

# img = cv2.imread('/data1/lhw/workspace/OCR_Test/发票小图/bcpp (15).JPG')
# binary = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 25, 15)
# se = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# mask = cv2.dilate(binary, se)
# cv2.imwrite('/data1/lhw/workspace/OCR_Test/mask.png', mask)
# mask1 = cv2.bitwise_not(mask)
# binary = cv2.bitwise_and(img, mask)
# result = cv2.add(binary, mask1)
# cv2.imwrite('/data1/lhw/workspace/OCR_Test/result.png', result)

# gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
# dst = cv2.equalizeHist(img)
# cv2.imwrite('/data1/lhw/workspace/OCR_Test/result.png', dst)
# blur = cv2.GaussianBlur(img,(5,5),0)
# mask = img - blur
# result = img + mask
# result = cv2.cvtColor(result,cv2.COLOR_GRAY2RGB)


# OTSU
# ret1, th1 = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)
# cv2.imwrite('/data1/lhw/workspace/OCR_Test/OTSU.png', th1)

# 直方图正则化
# Maximg = np.max(img)
# Minimg = np.min(img)
#
# Omin, Omax = 0, 255
# a = float(Omax - Omin) / (Maximg - Minimg)
# b = Omin - a * Minimg
#
# O = a * img + b
# img = O.astype(np.uint8)
# cv2.imwrite('/data1/lhw/workspace/OCR_Test/tmp.png', img)

# 自适应阈值
# 高斯滤波
# img_blur=cv2.GaussianBlur(img,(5,5),5)

# 自适应阈值分割
# img_thresh=cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3)

# img_thresh_blur=cv2.adaptiveThreshold(img_blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3)
#
# cv2.imwrite('/data1/lhw/workspace/OCR_Test/adaptiveThreshold_BLUR.png', img_thresh_blur)
# print(img)
resize_img = cv2.normalize(img, dst=None, alpha=230, beta=20, norm_type=cv2.NORM_MINMAX)
# print(resize_img)
resize_img = cv2.bilateralFilter(src=resize_img, d=3, sigmaColor=200, sigmaSpace=10)
resize_img = cv2.filter2D(resize_img, -1, kernel=np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32))

cv2.imwrite('/data1/lhw/workspace/OCR_Test/tmp.png', resize_img)